<?php 
	namespace Models;

	class User
	{	
		private $app;
		private $access = 1;
		function __construct($app){
			$this->app = $app;
		}

		function getAll($sord = '', $sidx = '', $start = 0, $lim = ''){

			$arg_sql = array();

			$order = '';
			/*if ( isset( $sord ) && isset( $sidx ) ){
				$order = " ORDER BY ? ? ";
				$arg_sql[] = (string) $sidx;
				$arg_sql[] = (string) $sord;
			} */
			$limit = '';
			/*
			if  (  isset ( $start ) && isset ( $lim )  ){
				$order = " LIMIT ? ? ";
				$arg_sql[] = (int) $start;
				$arg_sql[] = (int) $lim;
			}
			*/

			$sql = "SELECT iduser user_id, user_login, user_pass  FROM user " . $order . $limit;
			//$res = 	$this->app['db']->fetchAll($sql);
			return $this->app['db']->fetchAll($sql, $arg_sql);

		}

		function getCount(){
			$sql = "SELECT COUNT(*) count FROM user";
			$res =  $this->app['db']->fetchAssoc($sql);			
			return $res['count'];
		}


		function getOne($id){
			$sql = "SELECT iduser user_id, user_login, user_pass  FROM user WHERE iduser = ? ";
			return $this->app['db']->fetchAssoc($sql, array( (int) $id ));	
		}

		function insert($data){
			$fields_name = array( "user_login", "user_pass");
			$arr_insert = array(
					"user_login" => $data["user_login"], 
					"user_pass" => $data["user_pass"]
				);
			$sql = "INSERT INTO user (user_login, user_pass) VALUES (? , md5(?) )";
			$id = $this->app['db']->lastInsertId();
			return  $this->getOne($id);
		}

		function update($data){
			$id = $data['user_id'];
			$fields_name = array( "user_login", "user_id");
			$val_arr = array($data['user_login'], $data['user_id']);
			$sql = "UPDATE user SET user_login =  ? WHERE iduser = ?";
			$this->app['db']->executeQuery($sql, $val_arr) ;
			return  $this->getOne($id);
		}

		function delete($data){
			if( isset($data['user_id']) ){
				$this->app['db']->delete('user', array('iduser'=>$data['user_id'] ));
			}
		}
	}
?>